package manager;

import lombok.extern.slf4j.Slf4j;
import org.json.JSONObject;

import service.WxServiceImpl;

import entity.template.*;
import util.Request;

import java.io.InputStream;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Map;

@Slf4j
public class TemplateMessage {

    static InputStream in= TemplateMessage.class.getClassLoader().getResourceAsStream("wx-config.xml");
    static Map<String,String> Xmlmap= WxServiceImpl.parseRequest(in);
    private static final String APPID=Xmlmap.get("appidTest");
    private static final String SECRT=Xmlmap.get("secrtTest");
    private static final String GET_TOKEN_URL=Xmlmap.get("getToken");
    private static final String SET_INDUSTRY=Xmlmap.get("tmSetIndustry");
    private static final String SEND_URL=Xmlmap.get("sendTMUrl");
    private static final String TM_ID_S=Xmlmap.get("tmIdS");
    private static final String TM_ID_F=Xmlmap.get("tmIdF");



    public static void main(String[] args) {
        String url=SET_INDUSTRY.replace("ACCESS_TOKEN", WxServiceImpl.getAccessToken());
        JSONObject jsonObject=new JSONObject(new SetIndustry("2","41"));
        String rusult= Request.sendPost(url,jsonObject.toString());
        System.out.println(rusult);
    }

    public void sendTemplateMessage(Map<String,String>map){
        String tmId="";
        LocalDateTime dateTime = LocalDateTime.now();
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        String url=SEND_URL.replace("ACCESS_TOKEN",WxServiceImpl.getAccessToken());
        First first=new First(map.get("title"));
        Keyword1 keyword1=new Keyword1(map.get("content"));
        Keyword2 keyword2=new Keyword2(dateTime.format(formatter));
        Remark remark=new Remark("Golxen");
        WeishaoData weishaoData =new WeishaoData(first,keyword1,keyword2,remark);
        if (!map.get("content").contains("成功"))
            tmId=TM_ID_F;
        else
            tmId=TM_ID_S;
        WeishaoTM weishaoTM=new WeishaoTM(map.get("token"),tmId, weishaoData);
        JSONObject jsonObject=new JSONObject(weishaoTM);
        String result= Request.sendPost(url,jsonObject.toString());
        log.info(result);
    }

    public void sendRsmsTemplateMessage(Map<Integer,Map<String,Object>> map){
        String tmId=Xmlmap.get("tmIdRsms");
        String url=SEND_URL.replace("ACCESS_TOKEN",WxServiceImpl.getAccessToken());
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");

        for (Map.Entry<Integer,Map<String,Object>> entry:map.entrySet()){
            First first=new First("您门店昨日的交易概况如下");
            Keyword1 keyword1=new Keyword1(LocalDateTime.parse(entry.getValue().get("date").toString()).format(formatter));
            Keyword2 keyword2=new Keyword2(String.valueOf(entry.getValue().get("transactionsNum"))+"笔");
            Keyword3 keyword3=new Keyword3(String.valueOf(Integer.parseInt(entry.getValue().get("newCustomerNum").toString())
                    +Integer.parseInt(entry.getValue().get("oldCustomerNum").toString()))+"人");
            Keyword4 keyword4=new Keyword4(String.valueOf(entry.getValue().get("salesVolume"))+"元");
            Keyword5 keyword5=new Keyword5("0");
            Remark remark=new Remark("其中新顾客"+entry.getValue().get("newCustomerNum")+"人,老顾客"+entry.getValue().get("oldCustomerNum")+"人");
            SendBusinessData sendBusinessData =new SendBusinessData(first,keyword1,keyword2,keyword3,keyword4,keyword5,remark);

            SendBusinessTM sendBusinessTM=new SendBusinessTM(entry.getValue().get("openId").toString(),tmId, sendBusinessData);
            JSONObject jsonObject=new JSONObject(sendBusinessTM);
            String result= Request.sendPost(url,jsonObject.toString());
            log.info(result);
        }

    }
}
